package com.bw.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Created by qpx on 2018/5/11.
 */
@Configuration
public class RedisConfig {


    @Autowired
    AppConfig appConfig;

   @Bean
   public JedisConnectionFactory jedisConnectionFactory(){
       return new JedisConnectionFactory();
    }


    @Bean
    public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory){
       RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
      // redisTemplate.setConnectionFactory(jedisConnectionFactory);
       redisTemplate.setKeySerializer(new StringRedisSerializer());
       redisTemplate.setValueSerializer(new RedisObjectSerializer());
       redisTemplate.setConnectionFactory(appConfig.connectionFactory());
       return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(JedisConnectionFactory jedisConnectionFactory){
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
       // redisTemplate.setConnectionFactory(jedisConnectionFactory);
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(new RedisObjectSerializer());
        redisTemplate.setConnectionFactory(appConfig.connectionFactory());
        return redisTemplate;
    }
}
